<?php
namespace app\controllers;

use app\models\User;
use yii\web\Controller;

class LoginController extends Controller{
    public $enableCsrfValidation = false;
    public function actionLogin(){
        $post = \Yii::$app->request->post('form');
        $user_admin = $post['user_admin'];
        $user_pass = $post['user_pass'];
        $model = User::find()->where('user_admin=:admin',[':admin' => $user_admin])->one();
        if(!$model){
            $result = ['result'=>0,'data'=>'用户名或密码错误，请重新操作'];
            return json_encode($result);
        }
        if(!\Yii::$app->security->validatePassword($user_pass,$model->user_pass)){
            $result = ['result'=>0,'data'=>'用户名或密码错误，请重新操作'];
            return json_encode($result);
        }
        \Yii::$app->user->login($model);
        $result = ['result'=>1,'data'=>'登录成功'];
        return json_encode($result);
    }

    public function actionIslogin(){
        if(\Yii::$app->user->isGuest){
            $result = ['result'=>0,'data'=>'请先登录'];
            return json_encode($result);
        }
        $result = ['result'=>1,'data'=>'登录成功'];
        return json_encode($result);
    }

    public function actionOut(){
        \Yii::$app->user->logout();
        $result = ['result'=>1,'data'=>'退出成功'];
        return json_encode($result);
    }
}